// TopMainFramePanel.cs
//
//  Copyright (C) 2008 Jacek Trubłajewicz
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using System.Drawing;
using System.Collections.Generic;
using System.IO;
using Mono.Unix;

namespace wxGadu
{
	public class TopMainFramePanel : wx.Panel
	{
		#region Id
		private enum Id
		{
			contactsListCtrl
		}
		#endregion
		
		#region controls
		private wx.ListCtrl contactsListCtrl;
		private wx.BoxSizer topSizer;
		#endregion

		#region data members
		private MainFrame mainFrame;
		private wx.Colour defaultBackgroundColor;
		#endregion
		
		public TopMainFramePanel(wx.Window parent, MainFrame mainFrame):
			base(parent)
		{
			this.mainFrame = mainFrame;
			
			DoControls();
			DoControlsProperties();
			DoLayout();

			EVT_SIZE(new  wx.EventListener(OnPanelResize));
			EVT_LIST_ITEM_SELECTED((int) Id.contactsListCtrl, new wx.EventListener(OnListItemSelected));
			EVT_LIST_ITEM_RIGHT_CLICK((int) Id.contactsListCtrl, new wx.EventListener(OnListItemRightClicked));
			EVT_LIST_ITEM_ACTIVATED((int) Id.contactsListCtrl, new wx.EventListener(OnListItemActivated));
		}

		private void DoControls()
		{
			
			contactsListCtrl = new wx.ListCtrl(this, (int) Id.contactsListCtrl, wx.Window.wxDefaultPosition,
			                                   new Size(200, 200), 
			                                   wx.ListCtrl.wxLC_NO_HEADER|
			                                   wx.ListCtrl.wxLC_REPORT|
			                                   wx.Window.wxVSCROLL|
			                                   wx.Window.wxHSCROLL|
			                                   wx.Window.wxBORDER_MASK
			                                 );
			                                   /*wx.ListCtrl.wxLC_SMALL_ICON)*/
			defaultBackgroundColor = contactsListCtrl.BackgroundColour;
		}

		private void DoControlsProperties()
		{
			wx.ListItem header = new wx.ListItem("Contacts");
			//header.Image = -1;
			contactsListCtrl.InsertColumn(0, header);
			contactsListCtrl.SetColumnWidth(0, 250);
			contactsListCtrl.AssignImageList(mainFrame.ImageMgr.StatusImages, (int) wx.wxImageList.wxIMAGE_LIST_SMALL);
		}

		private void DoLayout()
		{
			topSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
			{
				topSizer.Add(contactsListCtrl, 1, wx.Stretch.wxEXPAND|wx.Direction.wxALL, 3);
			}
			this.Sizer = topSizer;
			topSizer.SetSizeHints(this);
			topSizer.Fit(this);
		}

		protected void OnPanelResize(object sender, wx.Event evt)
		{
			//TODO: calculate column width based on current font and string length
			contactsListCtrl.SetColumnWidth(0,  this.ClientSize.Width);
//			contactsListCtrl.SetColumnWidth(0,  1024);
			evt.Skip();
		}
			
		protected void OnListItemSelected(object sender, wx.Event evt)
		{
			wx.ListEvent e = (wx.ListEvent) evt;
			// send real postion of selection
			mainFrame.ContactSelected(e.Index);		
		}
		
		protected void OnListItemRightClicked(object sender, wx.Event evt)
		{
			wx.ListEvent e = (wx.ListEvent) evt;	
			// send real postion of selection
			mainFrame.ContactRightClicked(e.Index);			
		}
		
		protected void OnListItemActivated(object sender, wx.Event evt)
		{
			wx.ListEvent e = (wx.ListEvent) evt;
			// send real postion of selection
			mainFrame.ContactSelected(e.Index);	
			
			mainFrame.OnNewChatContextMenu(sender, evt);                                      
		}

		public wx.ListCtrl ContactsListCtrl
		{
			get { return contactsListCtrl; }
		}
		
		public void ContactListFontChanged(object sender)
		{
			this.EventHandler.AddPendingEvent(new wx.UpdateUIEvent((int) FakeId.contactListFontsChanged));
		}
		
		private void SetContactListColor()
		{
			if (mainFrame.AppCfg.UserConf.ContactListBackground != null)
				contactsListCtrl.BackgroundColour = (wx.Colour) mainFrame.AppCfg.UserConf.ContactListBackground;
			else
				contactsListCtrl.BackgroundColour = defaultBackgroundColor;
		}
		
		public void UpdateContactListColor(object sender, wx.Event evt)
		{
			SetContactListColor();
		}
		
		public void ContactListColorChanged(object sender)
		{
			this.EventHandler.AddPendingEvent(new wx.UpdateUIEvent((int) FakeId.contactListColorChanged));
		}
	}
}
